package com.zhisheng.gufei.web.action.produce;

import java.util.Date;
import java.util.Set;

import javax.annotation.Resource;

import com.demo2do.lighturl.annotation.Action;
import com.zhisheng.core.web.action.BaseAction;
import com.zhisheng.gufei.produce.entity.Audit;
import com.zhisheng.gufei.produce.entity.Declaration;
import com.zhisheng.gufei.produce.service.DeclarationService;
import com.zhisheng.gufei.utils.Constants;

public class AuditAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4066741443305472604L;
	
	@Resource
	private DeclarationService declarationService;
	
	private Declaration dec;
	private Audit audit;
	
	@Action("/produce/audit/waiting")
	@SuppressWarnings("unchecked")
	public String waiting() throws Exception{
		Set<String> auths = getCurrentUser().getRoleAuthoritiesSet();
		if(auths.contains("ROLE_ADMIN")){
			declarationService.findByPage(createPage(),Declaration.class);
		}else if(auths.contains("ROLE_AUDIT_T")){
			declarationService.getAuditPage(createPage(), Constants.Audit.AUDIT_T - 1);
		}else if(auths.contains("ROLE_AUDIT_C")){
			declarationService.getAuditPage(createPage(), Constants.Audit.AUDIT_C - 1);
		}else if(auths.contains("ROLE_AUDIT_P")){
			declarationService.getAuditPage(createPage(), Constants.Audit.AUDIT_P -1);
		}else{
			createPage();
		}
		return "waiting";
	}
	
	@Action("/produce/audit/checked")
	@SuppressWarnings("unchecked")
	public String checked() throws Exception{
        declarationService.getCheckedPage(createPage());
		return "checked";
	}
	
	@Action("/produce/audit/check")
	@SuppressWarnings("unchecked")
	public String audit() throws Exception{
		Declaration tdec = declarationService.findById(dec.getId());
		if(dec.getStatus()<0){
			tdec.setStatus(-tdec.getStatus()+dec.getStatus());
		}else{
			tdec.setStatus(tdec.getStatus()+dec.getStatus());
		}
		audit.setDeclaration(tdec);
		audit.setAuditor(getCurrentUser().getUsername());
		audit.setAuditdate(new Date());
		tdec.getAudits().add(audit);
		declarationService.saveOrUpdate(tdec);
		return "r:/produce/audit/waiting";
	}

	public Declaration getDec() {
		return dec;
	}

	public void setDec(Declaration dec) {
		this.dec = dec;
	}

	public Audit getAudit() {
		return audit;
	}

	public void setAudit(Audit audit) {
		this.audit = audit;
	}

}